package com.sprider.yjq.action;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import com.sprider.jsoup.service.SpriderAbstractService;
import com.sprider.jsoup.service.impl.InfoListSpriderService;
import com.sprider.jsoup.service.impl.InfoSpriderService;
import com.sprider.jsoup.service.impl.RegionSpriderService;
import com.sprider.model.RegionModel;

public class SpriderMain {
	public static void main(String[] args) {
		String key = "你好";
		FileOutputStream fos = null;

		SpriderAbstractService service = new RegionSpriderService();
		List<RegionModel> regionList = null;
		SpriderAbstractService infoListSeriver = null;
		try {
			fos = new FileOutputStream("D:/liebiao.txt");
			regionList = (List<RegionModel>) service.parseUrl("http://www.liebiao.com");

		} catch (IOException e) {
			e.printStackTrace();
		}
		if (regionList.size() == 0) {
			System.out.println("请求地址失败");
			return;
		}
		infoListSeriver = new InfoListSpriderService();

		try {
			for (RegionModel m : regionList) {
				String url = m.getRegionDomain() + "sou/" + key + "/";
				System.out.println(url);
				List<String> list = null;
				list = (List<String>) infoListSeriver.parseUrl(url);
				System.out.println("url:" + url);
				if (list.contains("http://beijing.liebiao.com/meirongbaojian/164318511.html")) {
					System.out.println(list);
				}
				InfoSpriderService infoSpriderService = new InfoSpriderService();
				for (String s : list) {
					String info = (String) infoSpriderService.parseUrl(s);
					System.out.println("info");
					fos.write((info + "/r/n").getBytes());
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}

	}
}
